package com.fractalwrench.harmonybootstrap.sample;

import static com.fractalwrench.harmonybootstrap.sample.ResourceTable.Layout_example_bootstrap_alert;

import com.beardedhen.harmonybootstrap.BootstrapAlert;
import com.beardedhen.harmonybootstrap.utils.LogUtil;
import com.beardedhen.harmonybootstrap.utils.UiUtil;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;

public class BootstrapAlertExample extends AbilitySlice implements Component.ClickedListener {
    final String TAG = "BootstrapAlertExample";
    BootstrapAlert alert;
    Button interactiveButton;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer rootLayout = (ComponentContainer)
                LayoutScatter.getInstance(getContext()).parse(Layout_example_bootstrap_alert, null, false);
        alert = (BootstrapAlert) rootLayout.findComponentById(ResourceTable.Id_dynamic_alert);

        if(alert != null) {
            alert.setVisibilityChangeListener(new BootstrapAlert.VisibilityChangeListener() {
                @Override
                public void onAlertDismissStarted(BootstrapAlert alert) {
                    LogUtil.debug(TAG, "Started dismissing alert!");
                }

                @Override
                public void onAlertDismissCompletion(BootstrapAlert alert) {
                    LogUtil.debug(TAG, "Finished dismissing alert!");
                }

                @Override
                public void onAlertAppearStarted(BootstrapAlert alert) {
                    LogUtil.debug(TAG, "Started appearing alert!");
                }

                @Override
                public void onAlertAppearCompletion(BootstrapAlert alert) {
                    LogUtil.debug(TAG, "Finished appearing alert!");
                }
            });
            alert.setClickedListener(this);
        }

        interactiveButton = (Button) rootLayout.findComponentById(ResourceTable.Id_interactive_button);
        interactiveButton.setClickedListener(this);
        setShapeElement(new UiUtil(this));
        super.setUIContent(rootLayout);
    }

    private void setShapeElement(UiUtil uiUtil) {
        interactiveButton.setBackground(uiUtil.getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_interactive_button:
                if(Component.HIDE == alert.getVisibility()) {
                    alert.show(true);
                }else{
                    alert.dismiss(true);
                }
                break;
            default:
                break;
        }
    }
}
